package pl.edu.romanow.snippet

import net.liftweb.http.{S, DispatchSnippet}
import org.jcp.xml.dsig.internal.dom.DOMUtils.NodeSet
import xml.NodeSeq
import net.liftweb.util.Helpers._
import pl.edu.romanow.util.HTMLUtil
import pl.edu.romanow.dispatch.FacebookDispatch
import net.liftweb.common.Full
import pl.edu.romanow.model.{CurrentUserJob, Facebook}
import pl.edu.romanow.dispatch.FacebookDispatch.FacebookSuccess

/**
 * Created by IntelliJ IDEA.
 * User: jar
 * Date: 21.04.12
 * Time: 13:06
 * To change this template use File | Settings | File Templates.
 */

class LoggedUserSnippet extends DispatchSnippet {


  override def dispatch = {
    case "facebook" => facebook _
  }


  def facebook(in: NodeSeq): NodeSeq = {

    if (Facebook.currentAccessToken.is.isEmpty) {
      ("#facebook_login_button [onclick]" #> HTMLUtil.ajaxCall(FacebookDispatch.ajaxRedirect(
        afterSuccess = Full(HTMLUtil.refresh)
      )))(in)
    }
    else {
      val fbUser = Facebook.currentUser.is match {
        case Full(u) => u
        case _ => return ("#facebook_login_button [onclick]" #> HTMLUtil.ajaxCall(FacebookDispatch.ajaxRedirect()))(in)
      }

      ("#facebook_login_button [onclick]" #> Facebook.showFacebookMenu &
        "#facebook_login_img [src]" #> fbUser.imgUrl &
        "#facebook_login_img [title]" #> ("Facebook " + fbUser.name)
        )(in)
    }
  }


}
